package http

import (
	"fmt"
	"path"
	"xrt/components/http/static"
	"xrt/constants"
	"xrt/core"
	"xrt/utils/file/asserts"
)

//go:generate xrt-assert -d $(pwd)/static/sources -t xHttp

type Default core.RouteSync
type xrtcss core.RouteSync
type E404 core.RouteSync
type E403 core.RouteSync

func (d *Default) Get(x *XHttp) {
	x.ParseBaseTemplate("XRT", `<span style="font-size: 12em;">XRT</span><br/>The extremely simple Web Container For Go. `)
}

func (d *Default) Default(x *XHttp) {
	x.WriteStringf("This is Default XRT [%s], Implement It.", x.Method)
}

func (d *xrtcss) ExtMatch() string {
	return "(.*)"
}

func (d *xrtcss) Get(x *XHttp) {
	switch x.Pattern[1] {
	case "/bootstrap.min.css":
		SetContentTypeFromExtension(x.Writer, ".css")
		x.WriteString(static.BootstrapV335)
	case "/xrt.css":
		SetContentTypeFromExtension(x.Writer, ".css")
		x.WriteString(static.BasePageCSS)
	case "/font/glyphicons-halflings-regular.woff2":
		x.WriteString(string(asserts.AssertFile(path.Join(constants.RootPath, "../xrt/components/http/static/sources/glyphicons-halflings-regular.woff2"))))
	default:
		x.RedirectTo(ERR404, true)
	}
}

func (r *E404) Default(x *XHttp) {
	x.ParseBaseTemplate("404", fmt.Sprintf(`<span style="font-size: 12em;">404</span><br/>Path [<font style="color: red;">%s</font>] Not Found. `, x.Path))
}

func (r *E403) Default(x *XHttp) {
	x.WriteString("哈哈、禁止访问了哦、大傻逼.")
}

func init() {
	core.X.RegisterRoutesSync(Default{}, xrtcss{}, E404{}, E403{})
}
